#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "functions.h"
#define TRUE 1
#define FALSE 0

LIST L;
void do_signed_magnitude()
{
    int number, neg = FALSE, count = 0;
    FILE *fp = fopen("in.txt", "r+");
    fscanf(fp, "%d", &number);
    if (number <= 63 && number >= -64)
    {
        if (number < 0)
        {
            neg = TRUE;
            number = -1 * number;
        }
        while (number != 0)
        {
            insert_left(number % 2, &L);
            ++count;
            number /= 2;
        }
        if (neg == FALSE)
            while (count < 8)
            {
                insert_left(0, &L);
                ++count;
            }
        else
        {
            while (count < 7)
            {
                insert_left(0, &L);
                ++count;
            }
            insert_left(1, &L);
            ++count;
        }
        print_list(L);
    }
    else
        printf("Signed magnitude: The number can not be represented on 8 bits.\n");
    fclose(fp);
}

void insert_left(int x, LIST *L)
{
    Tnode *tmp;
    tmp = (Tnode *)malloc(sizeof(Tnode));
    tmp -> info = x;
    tmp -> next = L -> first;
    L -> first = tmp;
}

void print_list(LIST L)
{
    FILE *fp1 = fopen("out.txt", "r+");
    Tnode *tmp = L.first;
    if(tmp == NULL)
        printf("list is empty");
    else
        for(; tmp != NULL; tmp = tmp -> next)
            fprintf(fp1, "%d", tmp -> info);
    fclose(fp1);
}

void undo_signed_magnitude()
{
    FILE *fp2 = fopen("out.txt", "r+");
    int c, p = 6, neg1 = FALSE, number = 0;
    c = fgetc(fp2);
    if (c == '1')
        neg1 = TRUE;
    while ((c = fgetc(fp2)) && p >= 0)
    {
        number += ((c - '0') * pow(2, p));
        --p;
    }
    if (neg1 == TRUE)
        number = -1 * number;
    printf("%d", number);
}

LIST L1;
void do_ones_complement()
{
    FILE *fp3 = fopen("in.txt", "r+");
    int number1, neg2 = FALSE, count1 = 0;
    fscanf(fp3, "%d", &number1);
    if (number1 < 0)
    {
        number1 = -1 * number1;
        neg2 = TRUE;
    }
    while (number1 != 0)
    {
        insert_left(number1 % 2, &L1);
        ++count1;
        number1 /= 2;
    }
    while (count1 < 8)
    {
        insert_left(0, &L1);
        ++count1;
    }
    if (neg2 == TRUE)
    {
        Tnode *tmp = L1.first;
        for (; tmp != NULL; tmp = tmp -> next)
            if (tmp -> info == 1)
                tmp -> info = 0;
            else
                tmp -> info = 1;
    }
    print_list(L1);
    fclose(fp3);
}

LIST L2;
void undo_ones_complement()
{
    FILE *fp4 = fopen("out.txt", "r+");
    int c, neg3 = FALSE, number = 0, p = 7;
    Tnode *tmp;
    c = fgetc(fp4);
    if (c == '1')
    {
        neg3 = TRUE;
        insert_right(1, &L2);
    }
    else
        --p;
    while ((c = fgetc(fp4)) != EOF)
    {
        insert_right(c - '0', &L2);
    }
    if (neg3 == TRUE)
    {
        for (tmp = L2.first; tmp != NULL; tmp = tmp -> next)
            if (tmp -> info == 1)
                tmp -> info = 0;
            else
                tmp -> info = 1;
    }
    for (tmp = L2.first; tmp != NULL && p >= 0; tmp = tmp -> next)
    {
        number += (tmp -> info * pow(2, p));
        --p;
    }

    if (neg3 == TRUE)
        number = -1 * number;

    printf("%d", number);
}

void insert_right(int x, LIST *L)
{
    Tnode *tmp;
    tmp = (Tnode *)malloc(sizeof(Tnode));
    tmp -> info = x;
    tmp -> next = NULL;
    if(L -> last != NULL)
    {
        L -> last -> next = tmp;
        L -> last = tmp;
    }
    else
    {
        L -> first = L -> last = tmp;
    }
}

